<template>
  <div class="components-sales-record">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="prodName"
            :label="$t('coupon.productName') + ':'"
          >
            <el-input
              v-model="searchForm.prodName"
              type="text"
              clearable
              :placeholder="$t('coupon.productName')"
            />
          </el-form-item>
          <el-form-item
            prop="nickName"
            :label="$t('distributionProdLog.distributor') + ':'"
          >
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('distributionProdLog.distributor')"
            />
          </el-form-item>
          <el-form-item
            prop="userMobile"
            :label="$t('users.phoneNumber') + ':'"
          >
            <el-input
              v-model="searchForm.userMobile"
              type="text"
              clearable
              :placeholder="$t('users.phoneNumber')"
            />
          </el-form-item>
          <el-form-item
            prop="state"
            :label="$t('distributionProdLog.distributionStatus') + ':'"
          >
            <el-select
              v-model="searchForm.state"
              clearable
              :placeholder="$t('distributionProdLog.distributionStatus')"
            >
              <el-option
                :label="$t('distribution.waitSettle')"
                value="1"
              />
              <el-option
                :label="$t('distributionProdLog.alreadySettled')"
                value="2"
              />
              <el-option
                :label="$t('distributionProdLog.invalidOrder')"
                value="-1"
              />
            </el-select>
          </el-form-item>
          <el-form-item
            prop="orderNumber"
            :label="$t('distribIncome.orderNumber') + ':'"
          >
            <el-input
              v-model="searchForm.orderNumber"
              type="text"
              clearable
              :placeholder="$t('distribIncome.orderNumber')"
            />
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="resetForm"
            >
              {{ $t('crud.emptyBtn') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con">
        <el-table
          ref="prodLogTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          style="width: 100%"
        >
          <el-table-column
            :label="$t('order.number')"
            prop="orderNumber"
            width="250"
          >
            <template #default="scope">
              <div class="table-cell-text">
                {{ scope.row.orderNumber }}
              </div>
            </template>
          </el-table-column>
          <!-- 商品信息 -->
          <el-table-column
            align="left"
            prop="prodName"
            :label="$t('product.prodInfo')"
            min-width="200"
          >
            <template #default="scope">
              <div class="table-cell-con">
                <div class="table-cell-image">
                  <ImgShow :src="scope.row.pic" />
                </div>
                <span class="table-cell-text">{{ scope.row.prodName }}</span>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distributionProdLog.commissionAmount')"
            prop="incomeAmount"
          />
          <el-table-column
            :label="$t('distributionProdLog.distributor')"
            prop="nickName"
          />
          <el-table-column
            :label="$t('users.phoneNumber')"
            prop="userMobile"
          />
          <el-table-column
            :label="$t('distributionProdLog.distributionStatus')"
            prop="state"
            align="center"
          >
            <template #default="scope">
              <span
                v-if="scope.row.state === 0"
                type="info"
              >{{
                $t('distribution.waitPayment')
              }}</span>
              <span v-if="scope.row.state === 1">{{ $t('distribution.waitSettle') }}</span>
              <span v-else-if="scope.row.state === 2">{{
                $t('distributionProdLog.alreadySettled')
              }}</span>
              <span v-else-if="scope.row.state === -1">{{ $t('distribution.invOrder') }}</span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const searchFormRef = ref(null)
let tempSearchForm = null // 保存上次点击查询的请求条件

const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const dataList = ref([])
const searchForm = reactive({
  prodName: '',
  nickName: '',
  userMobile: '',
  state: '',
  orderNumber: ''
})
onMounted(() => {
  getDataList()
})

// 获取数据列表
const getDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/distribution/distributionProd/getDistributionProdLogPage'),
    method: 'get',
    params: http.adornParams(
      Object.assign(
        {
          current: pageParam ? pageParam.currentPage : 1,
          size: pageParam ? pageParam.pageSize : 10
        },
        tempSearchForm
      )
    )
  }).then(({ data }) => {
    page.total = data.total
    page.pageSize = data.size
    page.currentPage = data.current
    dataList.value = data.records
  })
}
const onSearch = (newData = false) => {
  getDataList(page, newData)
}
const onPageSizeChange = val => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = val => {
  page.currentPage = val
  getDataList(page)
}
const resetForm = () => {
  searchFormRef.value.resetFields()
}
</script>
<style scoped>
div ::v-deep(.avue-crud__menu) {
  overflow: inherit;
  min-height: 0;
}
div ::v-deep(.avue-crud__right) {
  position: absolute;
  right: 0;
  top: -58px;
}
div ::v-deep(.avue-crud__pagination) {
  margin-bottom: 17px;
}
</style>
